package club.xinghe.servlet.teacher;

import club.xinghe.biz.TkBiz;
import club.xinghe.entity.Teacher;
import club.xinghe.entity.Tk;
import club.xinghe.util.JsonServlet;
import club.xinghe.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题库管理：分页显示，题型查询
 * @刘艺广
 */
@WebServlet("/teacher/TkMgrServlet")
public class TkMgrServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取当前页,每页条数,要查询的试题类型和关键次词
        int pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));
        int styleId=Integer.parseInt(request.getParameter("styleId"));
        String keyword=request.getParameter("keyword");

        HttpSession session=request.getSession();
        //得到老师所在专业的id
         Teacher teacher= (Teacher) session.getAttribute("teacher");
        int zId=teacher.getZy().getzId();
        TkBiz tkBiz=new TkBiz();

        //获得每页试题列表。
        List<Tk> tkList=tkBiz.showTk(pageIndex,pageSize,styleId,zId,keyword);
        int count=0;
        if(keyword.equals("")){
            count=tkBiz.getSingleCount(zId,styleId);
        }else{
            count=tkBiz.getCountByKeyword(styleId,zId,keyword);
        }

        //获取总页数
        int totalPages= PageUtil.getTotalPages(count,pageSize);

        //将当前页，总条数，每页题库列表加到map里，用JSON工具类转为JSON格式
        Map<String,Object> map=new HashMap<>();
        map.put("pageIndex",pageIndex);
        map.put("totalPages",totalPages);
        map.put("currSize",pageSize);
        map.put("tkList",tkList);
        map.put("styleIdReturn",styleId);

        JsonServlet.getJson(request,response,map);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
